/**
 * @details: 命令行根节点类型
 * @brief: 
 * @version: 1.0
 * @author: liuri
 * @Date: 2021-10-05 03:36:22
 * @LastEditors: liuri
 * @LastEditTime: 2021-10-05 15:31:09
 * @FilePath: /TestProject/cPath/vtysh/cmd_node.c
 * @Copyright (C) 2021 liuri. All rights reserved.
 */

#include <stdio.h>
#include "cmd.h"



/* Standard command node structures. */
static struct cmd_node auth_node =
{
    AUTH_NODE,
    "Password: ",
};

static struct cmd_node view_node =
{
    VIEW_NODE,
    "%s> ",
};


static struct cmd_node auth_enable_node =
{
    AUTH_ENABLE_NODE,
    "Password: ",
};

static struct cmd_node enable_node =
{
    ENABLE_NODE,
    "%s# ",
};

static struct cmd_node config_node =
{
    CONFIG_NODE,
    "%s(config)# ",
    1
};



struct cmd_node *cmd_node_get(enum node_type ntype)
{
    struct cmd_node *cnode;

    /* cmd_init hasn't been called */
    if (!cmdvec)
    {
        fprintf (stderr, "%s called before cmd_init, breakage likely\n", __func__);
        return NULL;
    }

    cnode = vector_slot (cmdvec, ntype);
    if (cnode == NULL) 
    {
        fprintf (stderr, "Command node %d doesn't exist, please check it\n", ntype);
        // () (1);
        return NULL;
    }
    return cnode;
}

vector cmd_node_ele_vec_get(enum node_type ntype)
{
    struct cmd_node *cnode = cmd_node_get(ntype);
    if (cnode)
        return cnode->cmd_vector;
    return NULL;    
}


void install_node (struct cmd_node *node, int (*func) (struct vty *))
{
    vector_set_index (cmdvec, node->node, node);
    node->func = func;
    node->cmd_vector = vector_init (VECTOR_MIN_SIZE);
    // node->cmd_hash = hash_create (cmd_hash_key, cmd_hash_cmp);
}

void cmd_node_init()
{
     // 插入顶层节点类型
    install_node (&view_node, NULL);
    install_node (&enable_node, NULL);
    install_node (&auth_node, NULL);
    install_node (&auth_enable_node, NULL);
    install_node (&config_node, NULL);

    // 安装节点默认的命令
    install_default_basic(cmd_node_ele_vec_get(VIEW_NODE));
    install_default_basic(cmd_node_ele_vec_get(ENABLE_NODE));
    install_default_basic(cmd_node_ele_vec_get(CONFIG_NODE));

    // 接口命令初始化
    cmd_interface_init(cmd_node_ele_vec_get(VIEW_NODE));
}




